package org.example.lds240306.didi.test2;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    static class Employee {
        int id;
        int techLevel;
        List<Employee> subordinates = new ArrayList<>();

        Employee(int id, int techLevel) {
            this.id = id;
            this.techLevel = techLevel;
        }
    }

    private static Employee[] employees;
    private static int[] results;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        employees = new Employee[n + 1];
        results = new int[n + 1];

        for (int i = 1; i <= n; i++) {
            employees[i] = new Employee(i, 0);  // Initialize employees
        }

        for (int i = 1; i < n; i++) {
            int managerId = scanner.nextInt();
            employees[i].subordinates.add(employees[managerId]);
        }

        for (int i = 1; i <= n; i++) {
            employees[i].techLevel = scanner.nextInt();
        }

        for (int i = 1; i < n; i++) {
            results[i] = findNearestManager(i).id;
        }

        for (int i = 1; i < n; i++) {
            System.out.print(results[i] + (i < n - 1 ? " " : ""));
        }
    }

    private static Employee findNearestManager(int employeeId) {
        Employee employee = employees[employeeId];
        Employee current = employee.subordinates.get(0);  // Start with the direct manager
        int minDiff = Math.abs(employee.techLevel - current.techLevel);
        Employee closestManager = current;

        while (!current.subordinates.isEmpty()) {
            current = current.subordinates.get(0);
            int diff = Math.abs(employee.techLevel - current.techLevel);
            if (diff < minDiff) {
                minDiff = diff;
                closestManager = current;
            }
        }

        return closestManager;
    }
}
